﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DiscoveryLogic.Analytics.UI.Common.Wizard
{
    /// <summary>
    /// Interaction logic for Wizard.xaml
    /// </summary>
    public partial class Wizard : Window
    {
        #region events
        /// <summary>
        /// 
        /// </summary>
        public delegate void OnFinish(Dictionary<string,object> sharedVars);
        /// <summary>
        /// 
        /// </summary>
        public delegate void OnCancel();

        public event OnFinish Finish;
        public event OnCancel Cancel;
        #endregion

        #region fields
        private IWizardPageBase[] wizardPages;
        private IWizardPageBase currentPage;
        #endregion

        #region shared state 
        private Dictionary<string,object> _SharedVariables;
        /// <summary></summary>
        public Dictionary<string,object> SharedVariables
        {
            get
            {
                return this._SharedVariables;
            }
            set
            {
                this._SharedVariables = value;
            }
        }
        #endregion

        public Wizard(Dictionary<string,object> sharedVars, IWizardPageBase[] allPages, IWizardPageBase startPage)
        {
            InitializeComponent();
            this._SharedVariables = sharedVars;
            this.wizardPages = allPages;
            foreach(IWizardPageBase wizPage in allPages)
            {
                wizPage.Init(this);
            }
            this.currentPage = startPage;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            // this.WindowStyle = System.Windows.WindowStyle.None;
            this.ResizeMode = System.Windows.ResizeMode.CanResizeWithGrip;
            this.ShowPage(this.currentPage);
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            bool canMove = currentPage.MoveNextPage();
            if (canMove)
            {
                if (currentPage.NextPage == null)
                {
                    this.Close();
                    if (this.Finish != null)
                        this.Finish(this._SharedVariables);
                }
                else
                {
                    currentPage = currentPage.NextPage;
                    this.ShowPage(currentPage);
                }
            }
        }

        private void btnPrev_Click(object sender, RoutedEventArgs e)
        {
            if(currentPage.PrevPage !=null)
            {
                currentPage = currentPage.PrevPage;
                this.ShowPage(currentPage);
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            if (this.Cancel != null)
                this.Cancel();

            this.Close();
        }

        private void ShowPage(IWizardPageBase page)
        {
            this.pageContainer.Content = page;
            ((UserControl)page).Arrange(new Rect(new Size(this.pageContainer.ActualWidth,this.pageContainer.ActualHeight)));
            this.txtTitle.Text = page.Title;
            this.txtDesc.Text = page.Description;
            if (page.NextPage != null)
                this.btnNext.Content = "Next";
            else
                this.btnNext.Content = "Finish";
            if (page.PrevPage != null)
                this.btnPrev.IsEnabled = true;
            else
                this.btnPrev.IsEnabled = false;
        }

    }
}
